package cn.cnlee.app.ui.home.holder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.adapter.holder.AbsHolder;
import com.adapter.holder.AbsItemHolder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import cn.cnlee.app.R;
import cn.cnlee.app.glide.GlideRoundTransform;
import cn.cnlee.app.pojo.NewsDTO;
import cn.cnlee.app.util.StringUtil;

/**
 *
 */
public class NewsItemHolder extends AbsItemHolder<NewsDTO, NewsItemHolder.ViewHolder> {

    public NewsItemHolder(Context context) {
        super(context);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.news_item_layout;
    }

    @Override
    public ViewHolder createViewHolder(View view) {
        return new ViewHolder(view);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return super.onCreateViewHolder(inflater, parent);
    }

    @Override
    protected void onBindViewHolder(@NonNull NewsItemHolder.ViewHolder holder, @NonNull final NewsDTO item) {
        holder.coverIv.setVisibility(StringUtil.isBlank(item.defaultPicUrl) ? View.GONE : View.VISIBLE);
        if (!StringUtil.isBlank(item.defaultPicUrl)){
            Glide.with(mContext)
                    .load(item.defaultPicUrl)
                    .transform(new CenterCrop(), new GlideRoundTransform())
                    .into(holder.coverIv);
        }
        holder.titleTv.setText(item.text);
        holder.tagTv.setText(item.channelName);
        holder.hitsTv.setText(item.hits + "浏览");
        holder.timeTv.setText(item.time);
    }

    @Override
    protected void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        RecyclerView.LayoutParams clp = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        if (clp instanceof StaggeredGridLayoutManager.LayoutParams) {
            ((StaggeredGridLayoutManager.LayoutParams) clp).setFullSpan(true);
        }
    }

    static class ViewHolder extends AbsHolder {

        private ImageView coverIv;
        private TextView titleTv, tagTv, hitsTv, timeTv;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            coverIv = getViewById(R.id.iv_cover);
            titleTv = getViewById(R.id.tv_title);
            tagTv = getViewById(R.id.tv_tag);
            hitsTv = getViewById(R.id.tv_hits);
            timeTv = getViewById(R.id.tv_time);
        }
    }

}
